<?php

class DefaultController extends Zend_Controller_Action
{

	private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Base();

        /* Initialize action controller here */
    }

    public function indexAction()
    {
		//Zend_Loader::loadClass('Application_Default_Model_User');
		//new Default_Model_User();
		$user = $this->common->checklogin(false);
		
		$this->view->username = $user["aln"];
		$this->view->userid   = $user["ali"];
    }

	public function loginAction()
	{
	
		//echo date_default_timezone_get();
		//$mf = new Default_Model_Default();
		 //$fc = Zend_Controller_Front::getInstance();
		 //echo  $zcf->getBaseUrl();
		//exit;
	}

	public function checkloginAction()
	{
		if($this->request->isXmlHttpRequest() && $this->request->isPost()) {
			$username = $this->request->getParam("username");
			$password = $this->request->getParam("password");
			
			/*$adapter = new Zend_Auth_Adapter_DbTable(
				$this->db,
				"user",
				"username",
				"password"
			);*/
			$row = $this->db->fetchRow("SELECT userid From user where username = :username And password = :password", 
				array( 
					"username" => trim($username), 
					"password" => md5($password, false)
				 ));
			
			//$adapter->setIdentity($username);
			//$adapter->setCredential($password);
			
			//$auth   = Zend_Auth::getInstance();

			//$result = $auth->authenticate($adapter);
			//var_dump($result->getCode());
			if($row) {
				$set = array(
					"last_login" => $this->common->getGmtime(),
					"last_ip"    => $this->common->getIp(),
					"login_times"=> "login_times + 1"
				);

				$this->db->query("UPDATE user SET " 
					. " last_login = '" . $this->common->getGmtime() . "', "
					. " last_ip    = '" . $this->common->getIp() . "', "
					. " login_times = login_times + 1 "
					. " WHERE userid = '" . $row["userid"] . "';"
				);
				//$where = $this->db->quoteInto("userid = ?", $row["userid"]);
				//$rows_affected = $this->db->update("user", $set, $where);

				$user = new Zend_Session_Namespace('user');
				$user->ali = $row["userid"];
				$user->aln = $username;

				$this->common->PutData('{result:""}');
			}else{
				$this->common->PutData('{result:"",error:"登录失败，请重试或与管理员联系..."}');
			}
		}else{
			echo '{result:"",error:"非法的请求，请重试或与管理员联系..."}';
		}

		exit;
	}
	public function logoutAction() {
		session_destroy();
		exit;
	}
	public function checkpermitAction(){
		$this->common->checklogin();

		//auth
		exit;
	}

	public function usergetserverAction(){
		exit;
	}

	public function mainAction(){

	}


}